﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WoSoft.PACS.Data.DepartmentDoctor;
using WoSoft.PACS.Data.Models;
using DevComponents.DotNetBar;

namespace WoSoft.PACS.Win.Forms.SubForms
{
    public partial class FrmDepartmentInfo : FrmBase
    {
        string _departmentID = string.Empty;//部门ID
        private DepartmentDoctorData departmentDoctor = new DepartmentDoctorData();

        public FrmDepartmentInfo(string departmentID)
        {
            InitializeComponent();
            _departmentID = departmentID;
        }

        //加载窗口
        private void FrmDepartmentInfo_Load(object sender, EventArgs e)
        {

            var dt = departmentDoctor.GetAllList();

            this.cbParent.DataSource = dt;
            this.cbParent.ValueMember = "ID";
            this.cbParent.DisplayMembers = "简称";
            this.cbParent.ParentFieldNames = "ID,上级ID";
            this.cbParent.SelectedIndex = -1;

            //设置拼音自动显示
            this.SetSpellCodeControl(tbName, this.tbjm);

            //加载数据
            if (string.IsNullOrEmpty(this._departmentID)) return;//新增部门

            DepartmentModel departmentModel = departmentDoctor.GetDepartmentByID(_departmentID);

            this.tbName.Text = departmentModel.Name;
            this.tbjm.Text = departmentModel.Code;
            this.tbno.Text = departmentModel.No;
            try
            {
                this.cbParent.SelectedValue = departmentModel.ParentID;
            }
            catch
            {
                this.cbParent.SelectedIndex = -1;
            }
            this.tbTel.Text = departmentModel.Tel;
            this.tbRemark.Text = departmentModel.Remark;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.tbName.Text.Trim().Length == 0)
            {
                this.errorProvider1.SetError(this.tbName, "名称是必填写项目");
                return;
            }

            DepartmentModel model = new DepartmentModel()
            {
                //ID = Guid.NewGuid().ToString(),
                Name = this.tbName.Text.Trim(),
                Code = this.tbjm.Text.Trim(),
                No = this.tbno.Text.Trim(),
                Tel = this.tbTel.Text.Trim(),
                Remark = this.tbRemark.Text.Trim(),
                ParentID = this.cbParent.SelectedValue != null ? this.cbParent.SelectedValue.ToString() : string.Empty
            };

            if (string.IsNullOrEmpty(this._departmentID))//新增
            {
                model.ID = Guid.NewGuid().ToString();
                departmentDoctor.Insert(model);
                MessageBoxEx.Show("添加科室成功!", "科室", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else//修改
            {
                model.ID = this._departmentID;
                departmentDoctor.Update(model);
                MessageBoxEx.Show("修改科室成功!", "科室", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            this.DialogResult = DialogResult.OK;
        }
    }
}
